package com.shuyuntu.proposal.controller;

import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.entity.Role;
import com.shuyuntu.proposal.security.UseSecurity;
import com.shuyuntu.proposal.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by limeng on 16/7/21.
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleService roleService;

    public RoleService getRoleService() {
        return roleService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }


    /**
     * 获取角色列表
     * @return list
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    @UseSecurity(category = "0601")
    public List<Role> getRoleList(HttpSession session) {
        return roleService.getRoleList();
    }

    /**
     * 根据角色获取用户
     * @param id 角色id
     * @return list
     */
    @RequestMapping(value = "/usersByRoleId/{id}", method = RequestMethod.GET)
    @ResponseBody
    public List<Delegate> getDelegateByRole(@PathVariable int id) {
        return roleService.getDelegateByRole(id);
    }

    /**
     * 新建角色
     * @param reqMap 参数列表
     * @return map
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    @UseSecurity(category = "0601")
    public Map<String, Object> newRole(@RequestBody Map<String, Object> reqMap,
                                       HttpSession session) {
        Map<String, Object> resMap = new LinkedHashMap<>();

        String roleName = (String)reqMap.get("name");
        Role role = new Role();
        role.setName(roleName);
        if (roleService.insertNewRole(role) != -1) {
            resMap.put("state", true);
        } else {
            resMap.put("state", false);
            resMap.put("result", "数据库操作出现异常");
        }

        return resMap;
    }

    /**
     * 更改角色名字
     * @param reqMap 参数列表
     * @return map
     */
    @RequestMapping(value = "/name", method = RequestMethod.PUT)
    @ResponseBody
    @UseSecurity(category = "0601")
    public Map<String, Object> changeRoleName(
            @RequestBody Map<String, Object> reqMap,
            HttpSession session) {
        int roleId = (Integer) reqMap.get("id");
        String name = (String) reqMap.get("name");

        int res = roleService.changeRoleName(roleId, name);

        Map<String, Object> resMap = new LinkedHashMap<>();

        switch (res) {
            case -1:
                resMap.put("state", false);
                resMap.put("result", "数据库操作发生异常");
                break;
            case 0:
                resMap.put("state", true);
                break;
            case 1:
                resMap.put("state", false);
                resMap.put("result", "没找到对应的角色");
                break;
            case 2:
                resMap.put("state", false);
                resMap.put("result", "名字不允许为空");
                break;
            case 3:
                resMap.put("state", false);
                resMap.put("result", "“超级管理员”和“教师代表”两个角色不能被改名!");
                break;
        }

        return resMap;
    }

    /**
     * 删除角色
     * @param id 待删除角色id
     * @return map
     */
    @UseSecurity(category = "0601")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> deleteRole(@PathVariable int id,
                                          HttpSession session) {
        Map<String, Object> resMap = new LinkedHashMap<>();

        if (id == 0 || id == 1) {
            resMap.put("state", false);
            resMap.put("result", "“超级管理员”和“教师代表”两个角色不能被删除!");
        }

        int res = roleService.deleteRole(id);

        switch (res) {
            case 0:
                resMap.put("state", true);
                break;
            case 1:
                resMap.put("state", false);
                resMap.put("result", "没找到待删除角色");
                break;
            case 3:
                resMap.put("state", false);
                resMap.put("result", "“超级管理员”和“教师代表”两个角色不能被删除!");
                break;
            case -1:
                resMap.put("state", false);
                resMap.put("result", "数据库操作发生异常");
                break;
        }

        return resMap;
    }

    /**
     * 给角色分配用户
     * @param roleId 角色id
     * @param userIdList 用户id列表
     * @return map
     */
    @UseSecurity(category = "0601")
    @RequestMapping(value = "/addUserTo/{roleId}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addUserToRole(@PathVariable int roleId,
                                             @RequestBody List<String> userIdList,
                                             HttpSession session) {
        Map<String, Object> resMap = new LinkedHashMap<>();

        StringBuffer successCount = new StringBuffer();
        int res = roleService.addUserToRole(roleId, userIdList, successCount);

        switch (res) {
            case 0:
                resMap.put("state", true);
                resMap.put("successCount", Integer.parseInt(successCount.toString()));
                break;
            case 1:
                resMap.put("state", false);
                resMap.put("result", "没找到角色");
                break;
            case -1:
                resMap.put("state", false);
                resMap.put("result", "数据库操作发生异常");
                break;
        }

        return resMap;
    }

    /**
     * 更改角色权限
     * @param id 角色id
     * @param permissionList 权限列表
     * @return map
     */
    @UseSecurity(category = "0601")
    @RequestMapping(value = "/changePermissions/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> changePermissions(@PathVariable int id,
                                                 @RequestBody List<Integer> permissionList,
                                                 HttpSession session) {
        Map<String, Object> resMap = new LinkedHashMap<>();

        int res = roleService.changePermissions(id, permissionList);

        switch (res) {
            case 0:
                resMap.put("state", true);
                break;
            case 1:
                resMap.put("state", false);
                resMap.put("result", "没找到角色");
                break;
            case -1:
                resMap.put("state", false);
                resMap.put("result", "数据库操作发生异常");
                break;
        }

        return resMap;
    }

}
